Central Article

Using the Central Accordion Tab Component


Table of Contents

  1. Introduction
  2. Getting Started
  3. Adding Tabs to the Accordion Tab Component
  4. Determining When the User Selects a Tab
  5. Switching Between Views When the Selected Tab Changes
  6. Using the Accordion Tab Component for Application Layout
  7. Adding Favorite Tab Support
  8. Additional Resources

Switching Between Views When the Selected Tab Changes

Because tabs provide an interface for multiple related items, tabbed controls lend themselves to applications that contain multiple views. The tabs can be used to manage the state and presentation of the views.

This section provides a simple example that shows:

  1. How to store references to a view or movie clip within each tab.
  2. How to use the tab control to manage the state of the possible views.

I'll show you how to create a simple example that toggles the state of three simple movie clips. However, the example can easily be applied to managing more complex movie clips that have classes associated with them.

First you need to create the views or movie clips whose state will be controlled by the Accordion Tab component. Then write the necessary ActionScript code.

The article assumes that you are starting with the file you created in the addItem section. If not, just create a new movie with two layers named “Actions” and “accordion”. Drag an instance of the Accordion Tab component onto the accordion layer, and give it an instance name of “myTab”.

  1. Create three movie clips, and name them “red”, “green”, and “blue”.
  2. Open each one and draw a 100 x 100 square. The color of the square should match the clip’s name (that is red, green, or blue).
  3. Make sure that each square is at coordinate 0,0.
  4. Create three new layers on your timeline (above the accordion layer) and name them “red”, “green” and “blue”. If you like, you can place these within a folder to help organize them.
  5. Drag an instance of each clip onto its respective timeline.
  6. Give each clip an instance name that matches its symbol name (i.e. “red”, “green”, or “blue”).
  7. Finally, position each clip in the bottom left of the accordion tab and make sure that the x,y coordinates of each clip are exactly the same.

At this point, your timeline and stage should resemble Figure 4.

Timeline and Stage with view clips

Figure 4. Timeline and Stage with view clips

Now you can add the ActionScript that manages the application. I'll give you the code first, followed by a detailed discussion of the script.

function onActivate()
{
     green._visible = false;
     blue._visible = false;

     myTab.addItem("red", red);
     myTab.addItem("green", green);
     myTab.addItem("blue", blue);

     selectedTab = red;

     myTab.setChangeHandler("onTabChange");
}

function onTabChange()
{
     selectedTab._visible = false;

     var tab = myTab.getSelectedItem().data;

     tab._visible = true;
     selectedTab = tab;
}

Central.initApplication(this, this);

Basically, you make all of the clips that are not selected invisible (_visible = false). You then store a reference to each clip in its respective tab, and when a tab is selected, you toggle the visibility of the clips.

The item selected by default is the red clip. So you make the green and blue clips invisible:

     green._visible = false;
     blue._visible = false;

Next add the tabs to the component using the addItem method.

     myTab.addItem("red", red);
     myTab.addItem("green", green);
     myTab.addItem("blue", blue);

Notice that the data stored with each tab (the second parameter) is actually a reference to the tab’s respective clip.

You can also use the setDataProvider method, in which case the reference to the clip is stored in the data property.

Next create a new property called selectedTab. This property stores a reference to the currently selected (and thus visible) tab.

     selectedTab = red;

Finally, set the change handler for the component to the onTabChange function.

The onTabChange function gets the clip reference from the selected tab, and then based on that toggles the state of the clips.

First, it sets the selectedTab’s visibility to false.

     selectedTab._visible = false;

Then, it provides a reference to the clip from the newly selected tab. Make that clip visible, and then set it as the selected tab:

     var tab = myTab.getSelectedItem().data;
	 
     tab._visible = true;
     selectedTab = tab;

If you publish the application into Central and test it, the visible clip changes based on the selected tab.

Of course, in most cases the clips would be more complex and have classes associated with them. However, the code for switching between them would be the same.

Instead of toggling the visibility, you can also dynamically attach and remove the movie clips. The code is pretty much the same, except instead of setting the visibility, you attach the clip to be displayed, and remove the previously selected clip. The main advantage to this technique is that all of your clips do not have to be loaded into memory. For applications with many views, or views that encompass a lot of functionality, this can make a significant difference in performance and memory usage.

The following sections outline the pros and cons of each technique.

The Pros and Cons of Setting Visibility

Pros

  • Switching between views is relatively fast.
  • Views automatically maintain state.
  • You can access all views via ActionScript even if they are not currently displayed.
  • Views do not need to initialize when they are switched to.

Cons

  • Requires that all clips be loaded into memory (thus requires more memory).
  • Initial initialization takes longer, since each view has to initialize on load / activate.

The Pros and Cons of Dynamically Attaching and Removing Movie Clips

Pros

  • Uses less memory, because only active views are loaded at any one time.
  • Initial initialization is relatively faster, because only the selected item has to be initialized on load or activate.

Cons

  • Switching between views is relatively slower, because the view must initialize itself.
  • Not as efficient, since views must be initialized every time they are loaded.
  • Views do not automatically maintain their state. State must be explicitly managed via ActionScript.

You can use these pros and cons to help determine the technique best suited for your application.